<?php

class UserForm extends BaseForm {
  const FORM_NAME           = 'frm';

  const FIELD_USERNAME         = 'username';
  const FIELD_FIRST_NAME       = 'first_name';
  const FIELD_LAST_NAME        = 'last_name';
  const FIELD_EMAIL            = 'email';
  const FIELD_PASSWORD         = 'password';
  const FIELD_CONF_PASSWORD    = 'conf_password';
  const FIELD_USER_GROUP_ID    = 'user_group';
  const FIELD_STATUS           = 'status';

  private $isEdit   = false;

  public function  __construct($isEdit = false) {
    $this->isEdit = $isEdit;
    parent::__construct();
  }

  public function configure() {
    $arrStatusList    = StringUtil::getStatusArray();
    $arrUserGroup     = USER_GROUPTable::getInstance()->getUserGroupArray();
    
    $widget = array(
            self::FIELD_USERNAME        => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_FIRST_NAME      => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_LAST_NAME       => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_EMAIL           => new sfWidgetFormInputText(array(), array('maxlength' => 255)),
            self::FIELD_PASSWORD        => new sfWidgetFormInputPassword(array(), array('maxlength' => 255)),
            self::FIELD_CONF_PASSWORD   => new sfWidgetFormInputPassword(array(), array('maxlength' => 255)),
            self::FIELD_USER_GROUP_ID   => new sfWidgetFormSelect(array('choices' => $arrUserGroup), array('multiple' => false, 'expand' => true)),
            self::FIELD_STATUS          => new sfWidgetFormSelect(array('choices' => $arrStatusList), array('multiple' => false, 'expand' => true))
    );

    $labels = array(
            self::FIELD_USERNAME      => 'Username<req />',
            self::FIELD_FIRST_NAME    => 'First Name<req />',
            self::FIELD_LAST_NAME     => 'Last Name<req />',
            self::FIELD_EMAIL         => 'Email<req />',
            self::FIELD_PASSWORD      => 'Password<req />',
            self::FIELD_CONF_PASSWORD => 'Confirm Password<req />',
            self::FIELD_USER_GROUP_ID => 'User Group<req />',
            self::FIELD_STATUS        => 'Is Active<req />',

    );

    $this->setWidgets($widget);
    $this->widgetSchema->setLabels($labels);

    $validators = array(
            self::FIELD_FIRST_NAME      => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_FIRST_NAME])))),
            self::FIELD_LAST_NAME       => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_LAST_NAME])))),
            self::FIELD_EMAIL           => new sfValidatorEmail(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_EMAIL])), 'invalid' => LangUtil::getText('error_invalid', array($labels[self::FIELD_EMAIL])))),
            self::FIELD_USER_GROUP_ID   => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_USER_GROUP_ID])))),
            self::FIELD_STATUS          => new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_STATUS])))),
    );

    if (!$this->isEdit) {
      $validators[self::FIELD_PASSWORD]    = new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_PASSWORD]))));
      $validators[self::FIELD_USERNAME]    = new sfValidatorString(array('trim' => true), array('required' => LangUtil::getText('error_required', array($labels[self::FIELD_USERNAME]))));
    }

    $this->setValidators($validators);
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidator'))));

    $this->widgetSchema->setNameFormat(self::FORM_NAME . '[%s]');

    $this->validatorSchema->setOption('allow_extra_fields', true);
    $this->validatorSchema->setOption('filter_extra_fields', false);
  }

  public function postValidator($validator, $values) {
    if (!$this->isEdit && trim($values[self::FIELD_USERNAME]) != '') {
      $users = USERTable::getInstance()->findByUsername(trim($values[self::FIELD_USERNAME]));
      $error = new sfValidatorError($validator, 'Username already exists.');
      if (!empty ($users[0])) {
        throw new sfValidatorErrorSchema($validator, array(self::FIELD_USERNAME => $error));
      }
    }
    $error = new sfValidatorError($validator, 'Confirm Password incorrect.');
    if (trim($values[self::FIELD_CONF_PASSWORD]) && trim($values[self::FIELD_PASSWORD])) {
      if (trim($values[self::FIELD_CONF_PASSWORD]) !== trim($values[self::FIELD_PASSWORD])) {
        throw new sfValidatorErrorSchema($validator, array(self::FIELD_CONF_PASSWORD => $error));
      }
    }

    return $values;
  }

  public function populateData(USER $user) {
    $this->setDefault(self::FIELD_USERNAME, $user->getUsername());
    $this->setDefault(self::FIELD_FIRST_NAME, $user->getFirstName());
    $this->setDefault(self::FIELD_LAST_NAME, $user->getLastName());
    $this->setDefault(self::FIELD_EMAIL, $user->getEmail());
    $this->setDefault(self::FIELD_PASSWORD, $user->getPassword());
    $this->setDefault(self::FIELD_USER_GROUP_ID, $user->getUserGroupId());
    $this->setDefault(self::FIELD_STATUS, $user->getIsActive());
  }
}
?>